package com.example.androidrssreader;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class RssDetailActivity extends Activity {
	
	private TextView txtTitle;
	private TextView txtPubDate;
	private TextView txtFullText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rss_detail);
		// Show the Up button in the action bar.
		setupActionBar();
		
		Intent intent = this.getIntent();
		
		txtTitle = (TextView) this.findViewById(R.id.txtDetailRssTitle);
		txtTitle.setText(intent.getExtras().getString(MainActivity.PACKAGE_NAME+".RssItem.title"));

		// Checking if RssItem.link or RssItem.pdaLink that got from intent bundle are not empty
		// so changing style of title TextView to blue underlined. Link stored in linkString
		String linkString = null; 
		if (intent.getExtras().getString(MainActivity.PACKAGE_NAME+".RssItem.pdalink").length() > 0) {
			linkString = intent.getExtras().getString(MainActivity.PACKAGE_NAME+".RssItem.pdalink");
			txtTitle.setTextColor(Color.BLUE);
			txtTitle.setPaintFlags(txtTitle.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
		}
		else
			if (intent.getExtras().getString(MainActivity.PACKAGE_NAME+".RssItem.link").length() > 0) {
				linkString = intent.getExtras().getString(MainActivity.PACKAGE_NAME+".RssItem.link");
				txtTitle.setTextColor(Color.BLUE);
				txtTitle.setPaintFlags(txtTitle.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
			}
			else 
				txtTitle.setTextColor(Color.BLACK);

		// If linkString is not empty then adding OnClickListener that will open that link
		if (linkString.length() > 0) {
			final String finalLinkString = linkString;
			txtTitle.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					RssDetailActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(finalLinkString)));
				}
			});
		}
			

		txtPubDate = (TextView) this.findViewById(R.id.txtDetailRssPubDate);
		txtPubDate.setText(intent.getExtras().getString(MainActivity.PACKAGE_NAME+".RssItem.pubdate"));

		txtFullText = (TextView) this.findViewById(R.id.txtDetailRssFullText);
		txtFullText.setText(intent.getExtras().getString(MainActivity.PACKAGE_NAME+".RssItem.fulltext"));
				
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.rss_detail, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
